local list = Spring.GetUnitPieceList(unitID)
VFS.Include("luarules/utils/debug.lua")
--printTable ("Spring.GetUnitPieceList()", list)

local function getChildPieces(pieceID)
  local t = {}
  local info = Spring.GetUnitPieceInfo(unitID, pieceID)
  if (info and info.children) then
    for _, name in ipairs(info.children) do
      t[name] = getChildPieces(piece(name))
    end
  end
  return t
end
  
--printTable ("piece hierarchy:", getChildPieces(1)) 
 

--pieces
local base = piece("base")
  body = piece("hull")
	  local chimney1, chimney2 = piece("chimney1", "chimney2")
	  local wakeML, wakeMR = piece("wakeML", "wakeMR")  --middle left
    local wakeBL, wakeBR = piece("wakeBL", "wakeBR")  --back right
    local turretF1, mountF1, barrelF1a, barrelF1b, firepointF1a, firepointF1b = piece("turretF1", "mountF1", "barrelF1a", "barrelF1b", "firepointF1a", "firepointF1b")
    local turretF2, mountF2, barrelF2a, barrelF2b, firepointF2a, firepointF2b = piece("turretF1", "mountF1", "barrelF1a", "barrelF1b", "firepointF1a", "firepointF1b")
    local turretB1, mountB1, barrelB1a, barrelB1b, firepointB1a, firepointB1b = piece("turretB1", "mountB1", "barrelB1a", "barrelB1b", "firepointB1a", "firepointB1b")
    local turretB2, mountB2, barrelB2a, barrelB2b, firepointB2a, firepointB2b = piece("turretB1", "mountB1", "barrelB1a", "barrelB1b", "firepointB1a", "firepointB1b")
    local turretL1, mountL1, barrelL1, firepointL1 = piece("turretL1", "mountL1", "barrelL1", "firepointL1")
    local turretL2, mountL2, barrelL2, firepointL2 = piece("turretL2", "mountL2", "barrelL2", "firepointL2")
    local turretR1, mountR1, barrelR1, firepointR1 = piece("turretR1", "mountR1", "barrelR1", "firepointR1")
    local turretR2, mountR2, barrelR2, firepointR2 = piece("turretR2", "mountR2", "barrelR2", "firepointR2")
    local tubeL3, firepointL3a, firepointL3b = piece("tubeL3", "firepointL3a", "firepointL3b")
    local tubeR3, firepointR3a, firepointR3b = piece("tubeR3", "firepointR3a", "firepointR3b")

scriptParams = {
  smokePieces = { body },
  enginePieces = { chimney1, chimney2 },
  wakePieces = { wakeML, wakeMR, wakeBL, wakeBR },
  smokeDelay = 300,
  maxRockAngleX = 5,
  maxRockAngleZ = 1,
  rockOnMove = false,

  weapons = {
    [1] = {
	--turret, mount, barrel, firepoint =
      turretTurnSpeed = 40,
      mountTurnSpeed = 30,
      barrelRecoil = 6,
      rockForce = 500,
      minPitch = -20,
      restores = true,
      cegID = 1,
    },
    [2] = 1,
    [3] = 1,
    [4] = 1,
    [5] = {
      turretTurnSpeed = 70,
      mountTurnSpeed = 50,
      barrelRecoil = 3,
      rockForce = 200,
      minPitch = -20,
      restores = true,
      cegID = 1,
    },
    [6] = 5,
    [7] = 5,
    [8] = 5,    
    [9] = {
      turretTurnSpeed = 50,
      mountTurnSpeed = 50,
	    minPitch = -20,
	    restores = true,
    },
    [10] = 9,
  }
}

include "pre_scripts.lua"

setWeaponPieces(1, turret1, barrel1, barrel1, firepoint1)
setWeaponPieces(2, turret2, barrel2, barrel2, firepoint2)
setWeaponPieces(3, turret3, barrel3, barrel3, firepoint3)
setWeaponPieces(4, tube4, firepoint4a, {firepoint4a, firepoint4b}, {firepoint4a, firepoint4b} )
setWeaponPieces(5, tube5, firepoint5a, {firepoint5a, firepoint5b}, {firepoint5a, firepoint5b} )

include "scripts.lua"

local scriptCreate = script.Create
function script.Create()
  scriptCreate()
  Show (chimney1)
  Show (chimney2)
end

function script.Killed (recentDamage, maxHealth)
  local severity = recentDamage / maxHealth

  Explode (body, SFX.NONE)
  if (severity <= 0.5) then
	  return 1
  elseif (severity <= 1.0) then
	  return 2
  else
  	return 3
  end
end